package com.antgroup.seckill.business.goods.controller;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 货架生成
 * @author wangxy
 * @date 2024/10/20 15:13
 */
public class ShelfCodeGenerator {
    public static void main(String[] args) {
        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个新的工作表
        Sheet sheet = workbook.createSheet("Shelf Codes");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        for (int pos = 1; pos <= 8; pos++) {
            Cell headerCell = headerRow.createCell(pos);
            headerCell.setCellValue("Position " + pos);
        }

        // 创建样式
        CellStyle boldStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true); // 加粗
        font.setFontHeightInPoints((short) 14); // 字体大小
        boldStyle.setFont(font);

        // 控制货架编号的参数
        int startShelf = 1;
        int startLayer = 1;
        int endShelf = 25;
        int endLayer = 5;
        int endPosition = 8;

        // 生成货架编码
        int rowIdx = 1; // 第一行是表头，从第二行开始
        for (int shelf = startShelf; shelf <= endShelf; shelf++) {
            for (int layer = startLayer; layer <= endLayer; layer++) {
                Row dataRow = sheet.createRow(rowIdx++);
                for (int position = 1; position <= endPosition; position++) {
                    Cell cell = dataRow.createCell(position);
                    cell.setCellValue(shelf + "-" + layer + "-" + position);
                    cell.setCellStyle(boldStyle); // 应用样式
                }
            }
        }

        // 输出Excel文件
        try (FileOutputStream outputStream = new FileOutputStream("shelf_codes.xlsx")) {
            workbook.write(outputStream);
            System.out.println("Excel file has been created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
